package com.imageFilters;

import android.graphics.Bitmap;


public class SepiaFilter implements ImageFilterInterface{
	private ImageData imageIn;
	
	public ImageData imageProcess(Bitmap bmp) {
		this.imageIn = new ImageData(bmp);
		return imageProcess();
	}
	/**
	 * Sets the RGB between 0 and 255
	 *
	 * @param a
	 * @return
	 */
	private int truncate(int a) {
		if (a < 0)
			return 0;
		else if (a > 255)
			return 255;
		else
			return a;
	}

	public ImageData imageProcess() {
		int r, g, b, depth, corfinal;
		int width    = imageIn.getWidth();
		int height   = imageIn.getHeight();
		depth = 60;
		for (int x = 0; x < imageIn.getWidth(); x++) {
			for (int y = 0; y < imageIn.getHeight(); y++) {
				r = imageIn.getRComponent(x, y);
				g = imageIn.getGComponent(x, y);
				b = imageIn.getBComponent(x, y);
				corfinal = (r + g + b) / 3;
				r = g = b = corfinal;
				r = truncate(r + (depth * 2));
				g = truncate(g + depth);
				imageIn.setPixelColor(x, y, r, g, b);
			}
		}
		return imageIn;
	}
	public SepiaFilter() {
		super();
	}
}
